package com.tangcl.module_commom.base

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.tangcl.module_base.log.LogUtils
import org.greenrobot.eventbus.EventBus

/**
 * Created by TCL on 2021/5/8
 *
 * description:
 */
abstract class BaseActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(getLayoutId())
        //初始化数据
        initData()
        //view与数据绑定
        initView()
        //设置监听
        setListener()
    }

    protected open fun initData() {
    }

    protected open fun initView() {

    }

    protected open fun setListener() {
    }

    abstract fun getLayoutId(): Int

    override fun onStart() {
        super.onStart()
        if (isRegisterEventBus()) {
            if (!EventBus.getDefault().isRegistered(this)) {
                EventBus.getDefault().register(this);
            }
        }
    }

    override fun onStop() {
        super.onStop()
        if (isRegisterEventBus()) {
            if (EventBus.getDefault().isRegistered(this)) {
                EventBus.getDefault().unregister(this);
            }
        }
    }

    override fun onResume() {
        super.onResume()
        LogUtils.v("onResume", "" + getTagName())
    }

    /**
     * 是否注册EventBus
     *
     * @return true绑定EventBus事件分发，默认不绑定，子类需要绑定的话复写此方法返回true.
     */
    protected open fun isRegisterEventBus(): Boolean {
        return false
    }

    protected open fun getTagName(): String {
        return this.javaClass.name
    }

}